#encoding:gbk
import time

#---- 参数设置 ----#
买入下单溢价 = 1.01  # 价格上浮1%
卖出下单溢价 = 0.99  # 价格下浮1%
stk_list = ['204001.SH', '131810.SZ']
交易时间 = '230500'  # 时分秒

def init(ContextInfo):
    # 填写股票的资金账号
    ContextInfo.accID = 'xxxxxxxx'
    back_price_type_dict = {0:'SALE5',1:'SALE4',2:'SALE3',3:'SALE2',4:'SALE1',5:'LATEST',6:'BUY1',
    7:'BUY2',8:'BUY3',9:'BUY4',10:'BUY5',12:'MARKET',13:'HANG',14:'COMPETE'}
    """
    style下单选价类型：LATEST最新,FIX指定,HANG挂单,COMPETE对手,MARKET涨跌停价,SALE5,SALE4,SALE3,SALE2,SALE1卖5-1,BUY1,BUY2,BUY3,BUY4,BUY5买1-5
    """
    '''
    下单选价类型：
    LATEST 最新, HANG 挂单, COMPETE 对手,MARKET 市价, 
    SALE5, SALE4, SALE3, SALE2, SALE1,   卖5-1
    BUY1, BUY2, BUY3, BUY4, BUY5         买5-1
    '''

    ContextInfo.start = '2024-11-06 00:00:00'
    ContextInfo.end = '2024-11-08 15:30:00'
    ContextInfo.run_time("handlebar_1","1nDay","2024-10-25 15:05:00")


def handlebar(ContextInfo):
    test(ContextInfo)
    pass

def test(ContextInfo):
    # '164824.SZ'
    # stk_list = ['204001.SH','131800.SZ']
    ticks = ContextInfo.get_full_tick(stk_list)
    # stk_list = ['002085.SZ']
    if ticks[stk_list[0]]['lastPrice'] > ticks[stk_list[1]]['lastPrice']:
        stk = stk_list[0]
    else:
        stk = stk_list[1]
    print(f"沪市({stk_list[0]})现价：{ticks[stk_list[0]]['lastPrice']}")
    print(f"深市({stk_list[1]})现价：{ticks[stk_list[1]]['lastPrice']}")
    print(f"当前选择标的：{stk}")
    pass

def handlebar_1(ContextInfo):
    ticks = ContextInfo.get_full_tick(stk_list)
    可用资金 = get_avaliable(ContextInfo.accID, 'STOCK')
    stk_amount = int(可用资金/1000) * 1000
    
    if ticks[stk_list[0]]['lastPrice'] > ticks[stk_list[1]]['lastPrice']:
        stk_price = ticks[stk_list[0]]['lastPrice'] * 卖出下单溢价
        stk_code = stk_list[0]
    else:
        stk_price = ticks[stk_list[1]]['lastPrice'] * 卖出下单溢价
        stk_code = stk_list[1]
    print(f'可用资金：{stk_amount}')
    # order_value('204001.SH', -stk_amount, 'fix', stk_price, ContextInfo, ContextInfo.accID)
    # passorder(24 , 1101, ContextInfo.accID, stk, 11, ticks[stk]['lastPrice'], 100, 1, ContextInfo)  #指定价格/股数立即下单
    # passorder(24 , 1102, ContextInfo.accID, '204001.SH', 5, -1, stk_amount, 1, ContextInfo)  #金额/最新价立即下单
    passorder(24 , 1102, ContextInfo.accID, stk_code, 11, stk_price, stk_amount, 1, ContextInfo)


# 获取持仓信息
def get_holdings(accountid, datatype):
    '''
    arg:
        accountid: 账户id, 
        datatype:
            'FUTURE':期货
            'STOCK':股票
            ......
    return:
    {股票名:{'手数':int, '持仓成本':float, '浮动盈亏':float, '可用余额':int}}
    '''
    PositionInfo_dict = {}
    resultlist = get_trade_detail_data(accountid, datatype, 'POSITION')
    for obj in resultlist:
        PositionInfo_dict[obj.m_strInstrumentID + '.' + obj.m_strExchangeID] = {
            '手数':obj.m_nVolume/100,
            '持仓成本':obj.m_dOpenPrice,
            '浮动盈亏':obj.m_dFloatProfit,
            '可用余额':obj.m_nCanUseVolume,
            '成交日期':obj.m_strOpenDate
        }
    return PositionInfo_dict

# 获取可用资金
def get_avaliable(accountid,datatype):
    result=0
    resultlist=get_trade_detail_data(accountid,datatype,"ACCOUNT")
    for obj in resultlist:
         result=obj.m_dAvailable
    return result